package com.wss.module.main.ui.page.flip;

import com.orhanobut.logger.Logger;
import com.wss.common.base.BaseActionBarAbility;
import com.wss.common.base.mvp.BasePresenter;
import com.wss.common.utils.PixelMapUtils;
import com.wss.module.main.ResourceTable;
import com.wss.module.main.ui.page.flip.adapter.ImageAdapter;
import com.wss.module.main.ui.page.flip.helper.ImagePiece;
import com.wss.module.main.ui.page.flip.helper.ImageSplitter;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorGroup;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.media.image.PixelMap;
import java.util.ArrayList;
import java.util.List;

public class CardFlipAbility extends BaseActionBarAbility {

    public static final int DISTANCE = 16000;

    ListContainer recyclerViewImage;
    ListContainer recyclerViewMantle;

    private List<ImagePiece> imageList = new ArrayList<>();
    private ImageAdapter imageAdapter;
    /**
     * 是否正在执行动画
     */
    private boolean isAnimator = false;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        initView();
    }

    @Override
    protected int getLayoutId() {
        return ResourceTable.Layout_ability_card_flip;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    private void initView() {
        setCenterText("卡片翻转");

        recyclerViewImage = (ListContainer) findComponentById(ResourceTable.Id_recycle_view_image);
        recyclerViewMantle = (ListContainer) findComponentById(ResourceTable.Id_recycle_view_mantle);

        PixelMap bitmap = PixelMapUtils.getPixelMapFromResource(getContext(), ResourceTable.Media_bb);

        if (bitmap == null) {
            return;
        }
        imageList.clear();
        imageList.addAll(ImageSplitter.split(bitmap, 5, 6));

        imageAdapter = new ImageAdapter(context, imageList, null);
        recyclerViewImage.setItemProvider(imageAdapter);

        //蒙层的适配器啥也没做只是为了适配底层图片格子
        ImageAdapter mantleAdapter = new ImageAdapter(context, imageList, new ImageAdapter.OnClickListener() {
            @Override
            public void onClick(int position, Image imageView) {
                if (isAnimator) {
                    return;
                }
                translateAnimOut(imageView);
                isAnimator = true;
                imageList.get(position).setAnim(true);
                imageAdapter.notifyDataChanged();
            }
        });
        mantleAdapter.setMantle(true);
        recyclerViewMantle.setItemProvider(mantleAdapter);
    }

    private void translateAnimOut(Image image) {
        AnimatorGroup group = new AnimatorGroup();
        AnimatorProperty rotationY = new AnimatorProperty();
        rotationY.setTarget(image);
        rotationY.rotate(180);

        AnimatorProperty alpha = new AnimatorProperty();
        alpha.setTarget(image);
        alpha.setDelay(500);
        alpha.alphaFrom(1).alpha(0);

        group.runParallel(rotationY, alpha);
        group.start();

        group.setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {

            }

            @Override
            public void onStop(Animator animator) {

            }

            @Override
            public void onCancel(Animator animator) {

            }

            @Override
            public void onEnd(Animator animator) {
                image.setAlpha(0);
                isAnimator = false;
            }

            @Override
            public void onPause(Animator animator) {

            }

            @Override
            public void onResume(Animator animator) {

            }
        });
    }
}
